package com.geeyuu.android.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;

import com.geeyuu.android.GeeConstants;
import com.geeyuu.android.R;
import com.geeyuu.android.utils.AndroidUtil;
import com.geeyuu.android.utils.LogUtil;

public class DialogActivity extends BaseActivity {

    private int type = -1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TAG = "DialogActivity";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_activity_layout);
        
        type = getIntent().getIntExtra("type", -1);
        if(type == -1){
            finish();
            return;
        }
        
        findViewById(R.id.take_photo).setOnClickListener(mOnClickListener);
        findViewById(R.id.pick_photo).setOnClickListener(mOnClickListener);
        
    }
    
    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.take_photo:
                takePhoto();
                break;

            case R.id.pick_photo:
                pickPhoto();
                break;
            }
        }
    };
    
    private static String photoFilePath = "";
    private static String cropFilePath = "";

    private static final int TAKE_PHOTO_REQUEST_CODE = 1;
    private static final int PICK_PHOTO_REQUEST_CODE = 2;
    private static final int CROP_PHOTO_REQUEST_CODE = 3;
    
    private void takePhoto() {
        if (AndroidUtil.getSdCardAvailSize() < AndroidUtil.MB10) {
            AndroidUtil.showMyToast(this, "手机存储空间不足.");
            setResult(88);
            finish();
            return;
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File cameraFile = new File(GeeConstants.PATH_TEMP_FILE_FOLDER, "take_temp");
        photoFilePath = cameraFile.getAbsolutePath();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraFile));
        startActivityForResult(intent, TAKE_PHOTO_REQUEST_CODE);
    }

    private void pickPhoto() {
        if (AndroidUtil.getSdCardAvailSize() < AndroidUtil.MB5) {
            AndroidUtil.showMyToast(this, "手机存储空间不足.");
            setResult(88);
            finish();
            return;
        }
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, PICK_PHOTO_REQUEST_CODE);
    }
    
    private void cropCameraImage(Uri photoUri) {
        Intent intent = AndroidUtil.getCropIntent(this);
        intent.setDataAndType(photoUri, "image/*");
        File cropLargeAvatarFile = new File(GeeConstants.PATH_TEMP_FILE_FOLDER, "crop_temp");
        cropLargeAvatarFile.delete();
        cropFilePath = cropLargeAvatarFile.getAbsolutePath();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropLargeAvatarFile));
        startActivityForResult(intent, CROP_PHOTO_REQUEST_CODE);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case TAKE_PHOTO_REQUEST_CODE:
                File photoFile = new File(photoFilePath);
                if (photoFile.exists() && photoFile.length() > 0) {
                    cropCameraImage(Uri.fromFile(photoFile));
                } else {
                    setResult(88);
                    finish();
                    LogUtil.e(TAG, "拍照回来获取照片失败");
                }
                break;
            case PICK_PHOTO_REQUEST_CODE:
                Uri photoUri = data.getData();
                if (photoUri != null) {
                    cropCameraImage(photoUri);
                } else {
                    setResult(88);
                    finish();
                    LogUtil.e(TAG, "选取照片回来获取Uri失败");
                }
                break;
            case CROP_PHOTO_REQUEST_CODE:
                File cropFile = new File(cropFilePath);
                if (cropFile.exists() && cropFile.length() > 0) {
                    Bitmap avatarBitmap = BitmapFactory.decodeFile(cropFilePath);
                    if (null != avatarBitmap) {
                        File realAvatarFile = new File(GeeConstants.PATH_LARGE_IMAGE_FOLDER, "photo_temp");
                        realAvatarFile.delete();
                        FileOutputStream fos = null;
                        FileOutputStream fos2 = null;
                        try {
                            realAvatarFile.createNewFile();
                            fos = new FileOutputStream(realAvatarFile);
                            avatarBitmap.compress(CompressFormat.JPEG, 60, fos);
                            
                            Bitmap thumb = AndroidUtil.getThumbBitmap(getApplicationContext(), avatarBitmap);
                            if(null != thumb){
                                File thumbFile = new File(GeeConstants.PATH_SMALL_IMAGE_FOLDER, "small_photo_temp");
                                thumbFile.delete();
                                fos2 = new FileOutputStream(thumbFile);
                                thumb.compress(CompressFormat.JPEG, 60, fos2);
                            }else{
                                LogUtil.e(TAG, "生成缩略图失败");
                            }
                            
                            Intent intent = new Intent();
                            intent.putExtra("filePath", realAvatarFile.getAbsolutePath());
                            intent.putExtra("type", type);
                            setResult(99, intent);
                            finish();
                        } catch (Exception e) {
                            setResult(88);
                            finish();
                            LogUtil.e(TAG, "压缩图片失败", e);
                        } finally {
                            if (null != fos) {
                                try {
                                    fos.close();
                                    fos = null;
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    } else {
                        LogUtil.e(TAG, "截图回来读取照片的bitmap为null");
                        AndroidUtil.showToast(getApplicationContext(), "手机内存不足，请在个人界面重新设置");
                        setResult(88);
                        finish();
                    }
                } else {
                    LogUtil.e(TAG, "截图回来获取照片失败");
                    AndroidUtil.showToast(getApplicationContext(), "截取图片失败，请在个人界面重新设置");
                    setResult(88);
                    finish();
                }
                break;
            }
        }
    }
    
}
